package retention_person

import (
	"fmt"
	"os"
	"time"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/retention_person"
	retention_personReq "github.com/flipped-aurora/gin-vue-admin/server/model/retention_person/request"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"github.com/xuri/excelize/v2"
	"go.uber.org/zap"
)

type CasRetentionPersonsApi struct{}

// CreateCasRetentionPersons 创建casRetentionPersons表
// @Tags CasRetentionPersons
// @Summary 创建casRetentionPersons表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body retention_person.CasRetentionPersons true "创建casRetentionPersons表"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /casRetentionPersons/createCasRetentionPersons [post]
func (casRetentionPersonsApi *CasRetentionPersonsApi) CreateCasRetentionPersons(c *gin.Context) {
	var casRetentionPersons retention_person.CasRetentionPersons
	err := c.ShouldBindJSON(&casRetentionPersons)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	casRetentionPersons.CreatedBy = utils.GetUserID(c)
	err = casRetentionPersonsService.CreateCasRetentionPersons(&casRetentionPersons)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteCasRetentionPersons 删除casRetentionPersons表
// @Tags CasRetentionPersons
// @Summary 删除casRetentionPersons表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body retention_person.CasRetentionPersons true "删除casRetentionPersons表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /casRetentionPersons/deleteCasRetentionPersons [delete]
func (casRetentionPersonsApi *CasRetentionPersonsApi) DeleteCasRetentionPersons(c *gin.Context) {
	ID := c.Query("ID")
	userID := utils.GetUserID(c)
	err := casRetentionPersonsService.DeleteCasRetentionPersons(ID, userID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteCasRetentionPersonsByIds 批量删除casRetentionPersons表
// @Tags CasRetentionPersons
// @Summary 批量删除casRetentionPersons表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /casRetentionPersons/deleteCasRetentionPersonsByIds [delete]
func (casRetentionPersonsApi *CasRetentionPersonsApi) DeleteCasRetentionPersonsByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	userID := utils.GetUserID(c)
	err := casRetentionPersonsService.DeleteCasRetentionPersonsByIds(IDs, userID)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateCasRetentionPersons 更新casRetentionPersons表
// @Tags CasRetentionPersons
// @Summary 更新casRetentionPersons表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body retention_person.CasRetentionPersons true "更新casRetentionPersons表"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /casRetentionPersons/updateCasRetentionPersons [put]
func (casRetentionPersonsApi *CasRetentionPersonsApi) UpdateCasRetentionPersons(c *gin.Context) {
	var casRetentionPersons retention_person.CasRetentionPersons
	err := c.ShouldBindJSON(&casRetentionPersons)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	casRetentionPersons.UpdatedBy = utils.GetUserID(c)
	err = casRetentionPersonsService.UpdateCasRetentionPersons(casRetentionPersons)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindCasRetentionPersons 用id查询casRetentionPersons表
// @Tags CasRetentionPersons
// @Summary 用id查询casRetentionPersons表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query retention_person.CasRetentionPersons true "用id查询casRetentionPersons表"
// @Success 200 {object} response.Response{data=retention_person.CasRetentionPersons,msg=string} "查询成功"
// @Router /casRetentionPersons/findCasRetentionPersons [get]
func (casRetentionPersonsApi *CasRetentionPersonsApi) FindCasRetentionPersons(c *gin.Context) {
	ID := c.Query("ID")
	recasRetentionPersons, err := casRetentionPersonsService.GetCasRetentionPersons(ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(recasRetentionPersons, c)
}

// GetCasRetentionPersonsList 分页获取casRetentionPersons表列表
// @Tags CasRetentionPersons
// @Summary 分页获取casRetentionPersons表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query retention_personReq.CasRetentionPersonsSearch true "分页获取casRetentionPersons表列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /casRetentionPersons/getCasRetentionPersonsList [get]
func (casRetentionPersonsApi *CasRetentionPersonsApi) GetCasRetentionPersonsList(c *gin.Context) {
	var pageInfo retention_personReq.CasRetentionPersonsSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := casRetentionPersonsService.GetCasRetentionPersonsInfoList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetCasRetentionPersonsPublic 不需要鉴权的casRetentionPersons表接口
// @Tags CasRetentionPersons
// @Summary 不需要鉴权的casRetentionPersons表接口
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /casRetentionPersons/getCasRetentionPersonsPublic [get]
func (casRetentionPersonsApi *CasRetentionPersonsApi) GetCasRetentionPersonsPublic(c *gin.Context) {
	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	casRetentionPersonsService.GetCasRetentionPersonsPublic()
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的casRetentionPersons表接口信息",
	}, "获取成功", c)
}

func (casRetentionPersonsApi *CasRetentionPersonsApi) GenerateExcel(c *gin.Context) {
	ID := c.Query("ID")
	person, err := casRetentionPersonsService.GetCasRetentionPersons(ID)
	if err != nil {
		global.GVA_LOG.Error("获取滞留人员信息失败", zap.Error(err))
		response.FailWithMessage("获取滞留人员信息失败", c)
		return
	}

	dicts, err := global.GetDictionarieInfo([]string{*person.Nation, *person.Culture, *person.MaritalStatus, *person.TownCode})
	if err != nil {
		global.GVA_LOG.Error("获取字典信息失败", zap.Error(err))
		response.FailWithMessage("获取字典信息失败", c)
		return
	}
	pwd, _ := os.Getwd()

	writer, err := excelize.OpenFile(pwd + "/templates/retention_single.xlsx")
	if err != nil {
		global.GVA_LOG.Error("打开模板文件失败", zap.Error(err))
		response.FailWithMessage("打开模板文件失败", c)
		return
	}
	if person.Name != nil {
		writer.SetCellValue("Sheet1", "B4", *person.Name)
	}
	if person.Gender != nil {
		writer.SetCellValue("Sheet1", "D4", *person.Gender)
	}
	if person.Age != nil {
		writer.SetCellValue("Sheet1", "B5", *person.Age)
	}
	if person.Nation != nil {
		writer.SetCellValue("Sheet1", "D5", dicts[*person.Nation].(string))
	}
	if person.Culture != nil {
		writer.SetCellValue("Sheet1", "F5", dicts[*person.Culture].(string))
	}
	if person.MaritalStatus != nil {
		writer.SetCellValue("Sheet1", "G6", dicts[*person.MaritalStatus].(string))
	}
	writer.SetCellValue("Sheet1", "F4", *person.IdNumber)
	if person.PassportNo != nil {
		writer.SetCellValue("Sheet1", "B6", *person.PassportNo)
	}
	writer.SetCellValue("Sheet1", "B8", *person.Address)
	if person.Phone != nil {
		writer.SetCellValue("Sheet1", "B10", *person.Phone)
	}
	if person.Phone != nil {
		writer.SetCellValue("Sheet1", "E6", *person.Phone)
	}

	if person.Retention != nil {
		writer.SetCellValue("Sheet1", "B32", *person.Retention)
		remark := fmt.Sprintf("综合以上研判情况，该人目前滞留%s，从事何工作未知，初步认定存在涉诈风险", *person.Retention)
		writer.SetCellValue("Sheet1", "B36", remark)
	}
	if *person.TownCode != "other_city" {
		writer.SetCellValue("Sheet1", "C37", "金溪县公安局"+dicts[*person.TownCode].(string)+"派出所")
	} else {
		writer.SetCellValue("Sheet1", "C37", "金溪县公安局刑侦大队")
	}

	writer.SetCellValue("Sheet1", "H37", time.Now().Format("2006年01月02日"))

	if person.Avatar != nil {
		err = writer.AddPicture("Sheet1", "H4", fmt.Sprintf("%s/%s", pwd, *person.Avatar), &excelize.GraphicOptions{
			AutoFit: true,
			OffsetX: 5,
			OffsetY: 2,
		})
		if err != nil {
			global.GVA_LOG.Error("读取头像失败", zap.Error(err))
			response.FailWithMessage("读取头像失败", c)
			return
		}
	}

	filename := fmt.Sprintf("uploads/tmp/risk_detail_%d_%d.xlsx", person.ID, time.Now().Unix())
	if err = writer.SaveAs(pwd + "/" + filename); err != nil {
		global.GVA_LOG.Error("保存文件失败", zap.Error(err))
		response.FailWithMessage("保存文件失败", c)
		return
	}

	response.OkWithDetailed(gin.H{
		"filename": filename,
	}, "获取成功", c)
}
